package cn.restcloud.example.spring.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.restcloud.example.spring.service.SpringUserService;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.context.AppContext;
import cn.restcloud.framework.core.util.DateTimeUtil;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbMapperUtil;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

/**
 * 使用RestCloud做为控制层，其他层使用springboot原生的java bean方法开发
 * @author Thinkpad
 */
@RestController
@RefreshScope
@BeanConfig(appId = "example",beanId = "TestSpringBootRest",beanName = "RestCloud与Springboot整合示例",beanType = BeanType.CONTROLLER)
public class TestSpringBootRest {
	
	//使用autowired注入springboot的java bean，在Controller控制层进行调用即可
	@Autowired
	private SpringUserService springUserService;
	
	//使用@Value方式注入属性
	@Value("${spring.devtools.restart.exclude}")
	private String value;
	
	@RequestMapping(value="/rest/example/springboot/getuser",method=RequestMethod.GET) //使用@RequestMapping注解
	@RestConfig(configName = "获取用户id") //使用@RestConfig注解API名称
	@Params({
		@Param(id = "userId", name = "用户名",required=true)
	})
	public DocAndView helloWorld(String userId) throws Exception{
		List<Document> docs=springUserService.getUserInfo(userId);
		RdbMapperUtil.selectList("EXAMPLE_SQL_181225001 ", "");
		PrintUtil.o("value=="+value);
		return ViewUtil.getJsonView(docs);
	}
	
	@RequestMapping(value="/",method=RequestMethod.GET) //使用@RequestMapping注解
	public String indexHtml() throws Exception{
		return "404未找到此API";
	}
	
	@RequestMapping(value="/springapi/test",method=RequestMethod.GET) //使用@RequestMapping注解
	public String sprintapitest(HttpServletResponse response) throws Exception{

		return "";
		
	}
	
}
